package com.nervenets.general.config;

import io.undertow.server.handlers.DisallowedMethodsHandler;
import io.undertow.util.HttpString;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Configuration;

@Configuration
public class UndertowWebServerCustomizerConfig implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> {
    @Override
    public void customize(UndertowServletWebServerFactory factory) {
        factory.addDeploymentInfoCustomizers(deploymentInfo -> {
            deploymentInfo.addInitialHandlerChainWrapper(handler -> {
                HttpString[] disallowedHttpMethods = {
                        HttpString.tryFromString("PUT"),
                        HttpString.tryFromString("MOVE"),
                        HttpString.tryFromString("COPYDELETE"),
                        HttpString.tryFromString("PROPFIND"),
                        HttpString.tryFromString("SEARCH"),
                        HttpString.tryFromString("MKCOL"),
                        HttpString.tryFromString("LOCK"),
                        HttpString.tryFromString("UNLOCK"),
                        HttpString.tryFromString("PROPPATCH"),
                        HttpString.tryFromString("TRACE"),
                        HttpString.tryFromString("TRACK")
                };
                return new DisallowedMethodsHandler(handler, disallowedHttpMethods);
            });
        });
    }
}
